#!/bin/bash

INSTALLDIR=${HOME}

build_tags() {
    cscope-indexer -r
    ctags --languages=c --langmap=c:.c.h --fields=+S -R .
}

remove_tags() {
    filelist="cscope.out cscope.out.in cscope.out.po cscope.files tags"
    for file in $filelist
    do
        if [ -f ".$file" ]; then
            rm -f ".$file"
        fi
    done
}

rename_tags_file() {
    filelist="cscope.out cscope.out.in cscope.out.po cscope.files tags"
    for file in $filelist
    do
        if [ -f "$file" ]; then
            mv $file ".$file"
        fi
    done
}

install_fzf(){
    fzf_install=$INSTALLDIR/.vim/plugged/fzf/install

    yes y | $fzf_install
}

install_fzf_view() {
    line="export FZF_DEFAULY_OPTS=\"--height 40% --layout=reverse --preview 'cat {} 2>/dev/null | head -500'\""
    file=$INSTALLDIR/.bashrc
    lno=""

    lno=$(\grep -nF "$line" "$file")

    if [ -z "$lno" ]; then
        echo >> "$file"
        echo "$line" >> "$file"
        echo >> "$file"
    fi
}

install_vimrc() {
    srcfile=/opt/xdtool/vimrc
    dstfile=$INSTALLDIR/.vimrc
    bakfile=$INSTALLDIR/.vimrc.bak

    if [ -f $dstfile ]; then
        if [ ! -f $bakfile ]; then
            mv $dstfile $bakfile
        fi
    fi

    cp -f $srcfile $dstfile
}

uninstall_vimrc() {
    dstfile=$INSTALLDIR/.vimrc
    bakfile=$INSTALLDIR/.vimrc.bak

    if [ -f $dstfile ]; then
        rm -rf $dstfile
    fi

    if [ -f $bakfile ]; then
        mv $bakfile $dstfile
    fi
}

install_plugged() {
    srcfile=/opt/xdtool/vim.tar.xz
    vimdir=$INSTALLDIR/.vim
    bakdir=$INSTALLDIR/.vim.bak

    if [ -d $vimdir ]; then
        if [ ! -d $bakdir ]; then
            mv $vimdir $bakdir
        fi
    fi

    rm -rf $vimdir
    tar -xvf $srcfile -C $INSTALLDIR
}

uninstall_plugged() {
    vimdir=$INSTALLDIR/.vim
    bakdir=$INSTALLDIR/.vim.bak

    if [ -d $vimdir ]; then
        rm -rf $vimdir
    fi

    if [ -d $bakdir ]; then
        mv $bakdir $vimdir
    fi
}

#install_ycm() {
#    # Check if the virtual environment already exists
#    if [ ! -d "$INSTALLDIR/ycm_venv" ]; then
#        python3 -m venv $INSTALLDIR/ycm_venv
#    fi
#
#    # Activate the virtual environment
#    source $INSTALLDIR/ycm_venv/bin/activate
#
#    # Upgrade pip
#    pip install --upgrade pip
#
#    # Install required packages if not already installed
#    pip show cmake || pip install $INSTALLDIR/.vim/python/cmake-*.whl
#    pip show py-cpuinfo || pip install $INSTALLDIR/.vim/python/py_cpuinfo-*.whl
#    pip show requests || pip install $INSTALLDIR/.vim/python/requests-*.whl
#
#    # Navigate to YouCompleteMe directory
#    cd $INSTALLDIR/.vim/plugged/YouCompleteMe
#
#    # Install YouCompleteMe with clangd completer if not already installed
#    if [ ! -f "third_party/ycmd/ycm_core.so" ]; then
#        ./install.py --clangd-completer
#    fi
#}

install_tools() {
    install_fzf_view
    install_vimrc
    install_plugged
    install_fzf
}

uninstall_tools() {
    uninstall_vimrc
    uninstall_plugged
}

main () {
    if [ $# -eq 0 ]; then
        build_tags
        rename_tags_file
    fi

    if [ $# -eq 1 ]; then
        case $1 in
        "r"|"-r")
            remove_tags;;
        "install")
            install_tools;;
        "uninstall")
            uninstall_tools;;
#        "ycm")
#            install_ycm;;
        *)
            echo "none";;
        esac
    fi
}

main $@

